$(document).ready(function () {
    var url = 'http://api.shur.cn/v1/skills/1?access_token=e3f9595e4559bceeda28514ba624b7ca290229c1&expand=userStatus';

    if (checkUrl(url)) {
        get(url, 'GET', {
            loading: function () {
                console.log('加载中。。。');
            },
            success: function (data) {
                console.log('回调-data:' + data);
                init(data);
            },
            error: function () {
                console.log('加载失败');
            }
        })
    }

    function init(data) {
        console.log('data:' + data);
        var title = data.title;
        var body = data.body;
        var icon = data.icon;
        var challenge = data.userStatus.challenges;
        var star = data.userStatus.star;
        var challenges = data.challenges;
        console.log('challenge:' + challenge);
        $('#badge-title').text(title);
        $('#badge-content').html(body);
        $('#badge-avatar').attr('src', 'img/icon/' + icon + '.png');
        if (challenges != "" && challenges != null && challenges != undefined) {
            console.log('challenges.size:' + challenges.length);
            $.each(challenges, function (index, item) {
                var id = item.id;
                var poster_url = item.poster_url;
                var title = item.title;
                var get_count = item.get_count;//完成人数
                var s_color = 'badge-item-btn ';
                var btn_text = '去挑战';

                $.each(challenge.split(','), function (index, item) {
                    if (id == item) {
                        s_color = 'badge-item-btn-ok';
                        btn_text = '已完成';
                    }
                });

                var i = index + 1;
                var s_id = "badge-item-"+index;
                var val = '<div class="badge-item" id='+s_id+'>\n' +
                    '        <img src=' + poster_url + ' alt="" class="badge-item-img">\n' +
                    '        <div class="badge-item-index">\n' +
                    '            <img src="img/bg_badge_stage_uncomplete.png" alt="" class="badge-item-icon">\n' +
                    '            <p class="badge-item-index-text"> ' + i + '</p>\n' +
                    '        </div>\n' +
                    '        <p class="badge-item-title">' + title + '</p>\n' +
                    '        <p class="completion-number-people">' + get_count + '超宝已完成</p>\n' +
                    '        <p class=' + s_color + '>' + btn_text + '</p>\n' +
                    '    </div>';

                $('#bottom').append(val);
                var badge = '';
                if (index == 0) {
                    $('#'+s_id).addClass('s-margin-left-15');
                } else if (index == challenges.length - 1) {
                    $('#'+s_id).addClass('s-margin-left-10');
                    var v = '<div class="seat"></div>';
                    $('#bottom').append(v);
                } else {
                    $('#'+s_id).addClass('s-margin-left-10');
                }
                console.log('badge:'+badge);
            });
            var value = 1;
            console.log("value:"+value);
            if (star!=0){
                value = star/challenges.length;
            }
            $("#circle1").circleChart({
                color: "#fff",
                backgroundColor: "#e6e6e6",
                background: true,
                speed: 2000,
                widthRatio: 0.2,
                value: value,
                unit: "percent",
                counterclockwise: false,
                size: 125,
                startAngle: 375,
                animate: true,
                backgroundFix: true,
                lineCap: "round",
                animation: "easeInOutCubic",
                text: false,
                redraw: false,
                cAngle: 0,
                textCenter: true,
                textSize: false,
                textWeight: "normal",
                textFamily: "sans-serif",
                relativeTextSize: 1 / 7,
                autoCss: true,
                onDraw: false
            });
        }

    }




});